/**
 * sushimeng
 */
//=================
/**
 * 判断是否是数字
 */
var checkIsNum = function(d){
	if(d.length<1){
		d="return null";
		return false;
	}
	for(var i=0; i<d.length; i++){
		if(d.charAt(i)<'0' || d.charAt(i)>'9')return false;
	}
	return true;
};
/**
 * jquery 方式清空表单
 * @param {Object} fid 表单id
 */
var doFormEmpty = function(fid){
	$('#'+fid+' :input').not(':button, :submit, :reset, :hidden').val('').removeAttr('checked').removeAttr('selected');	
}
//=====================
//通用的删除方法
var commonDel = function(url,param) {
	if(confirm('确认删除?')) {
		$.post(url,param,function(data) {
			var jsonData = $.parseJSON(data);
			if(!!jsonData['success']) {
				location.reload();
				return;
			}
				var jsonArray = [];
				for(var key in jsonData) {
					jsonArray.push(jsonData[key]);
				}
				alert(jsonArray.join('\r'));
		});
	}
}
//baseInfo update
var psave = function(){
	//validate
//	var validateRes = $("#baseInfoForm").triggerHandler("submit");
//	if(!validateRes) return false;
	//
	var bform = document.planteditorform;
	$.ajax({
            type: "post",
            cache:false,
            async: false,//同步
            url: bform.action,
            data: {
            _method:"psave"
            ,id:bform.id.value
            ,pname:bform.pname.value
            ,pdesc:CKEDITOR.instances['pdesc'].document.getBody().getHtml()
            },
            //dataType:"text",
            success: function(data, textStatus) {
            	if(checkIsNum(data)){
            		$("#planteditorform input[name='id']").attr("value",data);
            		$.openPopupLayer({
						name: "myStaticPopup",
						width: 300,
						target: "myHiddenDiv"
					});
            	}else{
            		alert(data);
            	}            	
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {                
                alert("出错！"+textStatus);
            }
        });
};

/**
 * 判断是否是时间间隔符
 * @param {Object} aa
 * @return {TypeName} 
 */
var isPoint = function(aa){
    	if(!aa || aa=="")return false;
    	if(aa=="." || aa=="." || aa=="。" || aa=="-" || aa=="/")return true;
    };//end function
/**
 * 标准化日期格式
 * @param {Object} input
 * @return {TypeName} 
 */
var writeDateFormat = function(input){
				if(!input || input=="")return;
				var dnum = 0;//小数点数目
				for(var i=0;i<input.length;i++){
					//alert(input[i]);
					if(isPoint(input[i])){
						if(i==input.length-1)continue;
						dnum = dnum + 1;
					}
				}
				var year="",mon="",day="";
				var yearok = false;
				var monok = false;
				var dayok = false;
				for(var i=0;i<input.length;i++){
					if(dnum==2 && !yearok){
						if(isPoint(input[i])){
							yearok = true;
							continue;
						}
						year = year + input[i];
						continue;
					}
					if(!monok){
						if(isPoint(input[i])){
							monok = true;
							continue;
						}
						mon = mon + input[i];
						continue;
					}
					if(isPoint(input[i]))break;
					day = day + input[i];
				}
				if(year==""){
					var nowDate = new Date();
					year = nowDate.getFullYear();
				}
				if(year.length==2)year = "20" + year;
				if(mon.length==1)mon = "0" + mon;
				if(day.length==1)day = "0" + day;
				 
   				return year+"-"+mon+"-"+day;
}//end function

//========================
//test
var suTest = function(){
//	$("[name='pdesc']").each(
//		function(){
//			alert("aa"+$(this).val());
//		}
//	);
	
//var editor = CKEDITOR.instances['pdesc'];
//    if (editor) { editor.destroy(true); }
//    CKEDITOR.replace('pdesc');
	alert("zz"+CKEDITOR.instances['pdesc'].document.getBody().getHtml());
}